package xu.ye.view.adapter;

import java.util.List;

import com.agent.crm.bean.MyContact;
import com.agent.crm.view.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class TypeExpendAdapter extends BaseExpandableListAdapter{
    private Context ctx;
    private List<List<MyContact>> typeContactData;
    private LayoutInflater mInflater = null;
    List<String> typeNameList = null;
    
    
    public TypeExpendAdapter(Context ctx, List<String> typeNameList, List<List<MyContact>> typeContactData){
        this.ctx = ctx;
        mInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.typeNameList = typeNameList;
        this.typeContactData = typeContactData;
    }
    
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView =  mInflater.inflate(R.layout.type_group_view, null);
        }
        GroupViewHolder groupView = new GroupViewHolder();
        groupView.mGroupName = (TextView) convertView.findViewById(R.id.typeGroupNameId);
        groupView.mGroupName.setText(typeNameList.get(groupPosition));
        convertView.setTag(R.id.xxx01, groupPosition);
        convertView.setTag(R.id.xxx02, -1);
        return convertView;
    }
    
    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.type_child_view, null);
        }
        ChildViewHolder childView = new ChildViewHolder();
        childView.mChildName = (TextView) convertView.findViewById(R.id.typeChildNameId);
        childView.mDetail = (TextView) convertView.findViewById(R.id.typeChildDetailId);
        
        childView.mChildName.setText(this.getChild(groupPosition, childPosition).getName());
        childView.mDetail.setText(this.getChild(groupPosition, childPosition).getCompany());
        
        convertView.setTag(R.id.xxx01, groupPosition);
        convertView.setTag(R.id.xxx02, childPosition);
        return convertView;
    }
    
    
    @Override
    public MyContact getChild(int groupPosition, int childPosition) {
        return typeContactData.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    

    @Override
    public int getChildrenCount(int groupPosition) {
        return typeContactData.get(groupPosition).size();
    }

    @Override
    public List<MyContact> getGroup(int groupPosition) {
        return typeContactData.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return typeContactData.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    

    @Override
    public boolean hasStableIds() {
        
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        /*很重要：实现ChildView点击事件，必须返回true*/
        return true;
    }
    
    private class GroupViewHolder {
        TextView mGroupName;
    }

    private class ChildViewHolder {
        TextView mChildName;
        TextView mDetail;
    }

    public void removeChild(int groupPosition, int position) {
        typeContactData.get(groupPosition).remove(position);
    }
    
    public void removGroup(int groupPosition){
        typeContactData.remove(groupPosition);
    }
    
    
}
